package com.depository.oss.provider;

import com.depository.oss.model.entity.User;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.jdbc.SQL;
import java.util.Map;
import java.util.Objects;
import static org.apache.ibatis.jdbc.SqlBuilder.*;

public class UserProvider {

    public String queryForPage() {
        BEGIN();
        SELECT("id id,login_name loginName,login_password loginPassword");
        FROM("user_info2");
        return SQL();
    }

    public String getUser(String id) {
        return "select id id,login_name loginName,login_password loginPassword from user_info2 where id=#{id}";
    }

    public String insertUser(User user) {
        SQL sql = new SQL();
        sql.INSERT_INTO("user_info2") ;
        if(StringUtils.isNotEmpty(user.getId()))
            sql.VALUES("id","'"+user.getId()+"'");
        if(StringUtils.isNotEmpty(user.getLoginName()))
            sql.VALUES("login_name","'"+user.getLoginName()+"'");
        if(StringUtils.isNotEmpty(user.getLoginPassword()))
            sql.VALUES("login_password","'"+user.getLoginPassword()+"'");
       return sql.toString();


    }

    public String updateUser(User user) {
        //User user = (User) para.get("user");

        /*return "UPDATE user_info2 SET login_name = '" + user.getLoginName() + "'," +
                " login_password = '" + user.getLoginPassword()  +"' WHERE" +
                String.format(" id = '%s';", user.getId());*/

        return new SQL() {{
            UPDATE("user_info2");
            SET("login_name = #{loginName} ");
            SET("login_password = #{loginPassword}");
            WHERE("id = #{id}");
        }}.toString();
    }
}
